<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: pi_clickandbuy_cancel.php 88 2013-09-21 12:04:13Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 88 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:04:13 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * @category  PayIntelligent
 * @package   PayIntelligent_ClickandBuy
 * @copyright (C) 2010 PayIntelligent GmbH  <http://www.payintelligent.de/>
 * @license   GPLv2
 */

require_once('includes/application_top.php');
include('../lang/'.$_SESSION['language'].'/admin/pi_clickandbuy.php');

require(DIR_FS_CATALOG.DIR_WS_CLASSES.'class.clickandbuy.php');
$cab = new clickandbuy();	

$shopOrderID = $_GET['oID'];

!empty($_GET['actionSave']) ? $actionSave = true : $actionSave = false;	

$authentication = $cab->getCabSettings('clickandbuy');

$cabOrderDetails = $db->db_query("SELECT * FROM ".DB_PREFIX."picab_orders WHERE shopOrderID ='".$shopOrderID."'");

$amount = (float)$cabOrderDetails->fields['amount'] ;
$refunded = (float)$cabOrderDetails->fields['refunded'];
$cancelled = (float)$cabOrderDetails->fields['cancelled'];
$openAmount = ($amount - $refunded - $cancelled);					
$externalID = 'NULL';

if($actionSave && $cancelled == 0.00) {										
	$transactionID = $cabOrderDetails->fields['transactionID'];
							
	$requestResult = call_user_func('cancelRequest',$authentication,$transactionID);			
	$requestValues = $requestResult['values'];

	if ($requestValues['transaction']['transactionStatus'] == 'CANCELLED') {
		$messageBox = 'SUCCESS';
		$cancelled = $openAmount + $cancelled;

		$db->db_query("UPDATE ".DB_PREFIX."picab_orders SET cancelled =  '".$cancelled."' WHERE shopOrderID ='".$shopOrderID."'");

		$transactionID = $requestValues['transaction']['transactionID'];
		$transactionStatus = $requestValues['transaction']['transactionStatus'];					
		$db->db_query("INSERT INTO ".DB_PREFIX."picab_transactions (id,shopOrderID,transactionID,externalID,transactionType,description,amount,currency,paid,status,created,modified) VALUES (NULL,'".$shopOrderID."','".$transactionID."','".$externalID."','cancel','".$itemDesc1."',".$cancelled.",'".$cabOrderDetails->fields['currency']."',1,'".$transactionStatus."',NOW(),NOW())");
	} else {
		$messageBox = 'ERROR';
		$errorDescription = $requestResult['values']['detail']['errorDetails']['description'];
	}
	
} elseif($actionSave && $cancelled > 0.00) {
		$messageBox = 'ERROR';
		$errorDescription = CLICKANDBUY_ORDER_CANCEL_IS_CANCELLED;
}		
		
$cancelled = number_format($cancelled, 2, '.', '');
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
	<tr>
		<td>
			<table border="0" width="100%" cellspacing="0" cellpadding="2" height="40">
				<tr>
					<td class="pageHeading"><?php echo CLICKANDBUY_ORDER_CLICKANDBUY.' '.CLICKANDBUY_ORDER_DETAILS_CANCELLATION; ?></td>
					<td class="pageHeading" align="right">
						<a class="button cab_button" href="pi_clickandbuy_details.php?oID=<?php echo $shopOrderID; ?>">
							<?php echo CLICKANDBUY_ORDER_DETAILS_BACK; ?>
						</a>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td>
			<?php 
				if ($messageBox == 'SUCCESS') {
					$showMessage = $cab->showMessageSuccess(CLICKANDBUY_ORDER_CANCEL_SUCCESSFUL);
					echo '<div class="clickandbuy_msg">'.$showMessage.'</div>';
				} elseif ($messageBox == 'ERROR') {
					$showMessage = $cab->showMessageError(CLICKANDBUY_ORDER_CANCEL_ERROR.' '.$errorDescription);
					echo '<div class="clickandbuy_msg">'.$showMessage.'</div>';
				}
			?>									        
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
				<tr>
					<td>
						<table cellspacing="0" cellpadding="5" border="0" width="98%">
							<tr>
								<td width="50%">							
									<table cellspacing="0" cellpadding="5" border="0" width="400">
										<tr>
											<td valign="top" class="infoBoxHeading"colspan="2">
												<b><?php echo CLICKANDBUY_ORDER_DETAILS_CANCELLATION; ?>: </b>
											</td>                      
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_TOTAL_AMOUNT; ?>:</td>
											<td class="infoBoxContent">
												<?php echo $cabOrderDetails->fields['amount'].' '.$cabOrderDetails->fields['currency']; ?>
											</td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_REFUNDED; ?>:</td>
											<td class="infoBoxContent">
												<?php echo $refunded.' '.$cabOrderDetails->fields['currency']; ?>
											</td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"><?php echo CLICKANDBUY_ORDER_DETAILS_CANCELLED; ?>:</td>
											<td class="infoBoxContent">
												<?php echo $cabOrderDetails->fields['cancelled'].' '.$cabOrderDetails->fields['currency']; ?>
											</td>
										</tr>
										<tr>
											<td class="infoBoxContent" height="20" width="120"></td>
											<td class="infoBoxContent">
												<a class="button cab_button" href="pi_clickandbuy_cancel.php?oID=<?php echo $shopOrderID; ?>&actionSave=true">
													<?php echo CLICKANDBUY_ORDER_CANCEL_CANCEL_NOW; ?>
												</a>
											</td>
										</tr>
									</table>
								</td>
							</tr>	
						</table>
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>